/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
 * Description: 
 * Author: g00421808
 * Create: 11/17/2022
 * Notes: 
 */

#ifndef JSBIND_NAPI_OVERLOADER_H
#define JSBIND_NAPI_OVERLOADER_H

#include <vector>
#include <node_api.h>

#include "jsbind/arg_storage/arg_storage.h"
#include "jsbind/config.h"

namespace Jsb {
typedef void (*FuncPtr)();
class JSBIND_EXPORT NapiOverloader {
public:
    typedef void (*CreateValueHandler)(napi_env env, napi_callback_info info, ArgStorage &storage);
    struct OverloadGroup {
        std::vector<FuncPtr> methods;
        napi_callback externalWrapper;
    };

    static napi_value Wrapper(napi_env env, napi_callback_info info);

    static napi_value CreateValue(napi_env env, napi_callback_info info);

    static int32_t AddGroup();

    static void AddMethod(int32_t groupId, int32_t arity, FuncPtr method);

    static inline OverloadGroup &GetGroup(int32_t groupId) {
        return (GetOverloaderGroups()[groupId]);
    }

    static void SetExternalWrapper(int32_t groupId, napi_callback externalWrapper) {
        GetGroup(groupId).externalWrapper = externalWrapper;
    }

    static std::vector<OverloadGroup>& GetOverloaderGroups();
};
} // namespace Jsb

#endif //JSBIND_NAPI_OVERLOADER_H
